/******************************************************************/
/*                                                                */
/*  Winpooch : Windows Watchdog                                   */
/*  Copyright (C) 2004-2005  Benoit Blanchon                      */
/*                                                                */
/*  This program is free software; you can redistribute it        */
/*  and/or modify it under the terms of the GNU General Public    */
/*  License as published by the Free Software Foundation; either  */
/*  version 2 of the License, or (at your option) any later       */
/*  version.                                                      */
/*                                                                */
/*  This program is distributed in the hope that it will be       */
/*  useful, but WITHOUT ANY WARRANTY; without even the implied    */
/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       */
/*  PURPOSE.  See the GNU General Public License for more         */
/*  details.                                                      */
/*                                                                */
/*  You should have received a copy of the GNU General Public     */
/*  License along with this program; if not, write to the Free    */
/*  Software Foundation, Inc.,                                    */
/*  675 Mass Ave, Cambridge, MA 02139, USA.                       */
/*                                                                */
/******************************************************************/


// module's interface
#include "Reason.h"

// standard headers
#include <tchar.h>
#include <stdio.h>

// project's header
#include "Assert.h"
#include "Rule.h"


/******************************************************************/
/* Internal data types                                            */
/******************************************************************/

typedef struct {
  LPCTSTR	szName ;
  UINT		nParams ;
  LPCTSTR	aParams[MAX_PARAMS] ;
} REASON_DESC ;


/******************************************************************/
/* Internal constants                                             */
/******************************************************************/

static REASON_DESC aReasons[_REASON_COUNT] = {
  { TEXT("Undefined"),		0 }, 
  { TEXT("File::Read"),		1, { TEXT("File name") } },
  { TEXT("File::Write"),	1, { TEXT("File name") } },
  { TEXT("Net::Connect"),	2, { TEXT("Address"), TEXT("Port") } },
  { TEXT("Net::Listen"),	2, { TEXT("Address"), TEXT("Port") } },
  { TEXT("Reg::SetValue"),	2, { TEXT("Key path"), TEXT("Value name") } },
  { TEXT("Sys::Execute"),	1, { TEXT("Command line") } }
} ;


/******************************************************************/
/* Exported function : GetName                                    */
/******************************************************************/

LPCTSTR Reason_GetName (UINT nReason) 
{
  if( nReason>=_REASON_COUNT )
    return TEXT("[Invalid reason]") ;

  return aReasons[nReason].szName ;
}


/******************************************************************/
/* Exported function : GetId                                      */
/******************************************************************/

UINT	Reason_GetId (LPCTSTR szReason) 
{
  UINT	nId ;

  for( nId=_REASON_COUNT-1 ; nId>0 ; nId-- )
    if( ! _tcsicmp(aReasons[nId].szName, szReason) )
      break ;

  return nId ;
}


/******************************************************************/
/* Exported function : GetParamCount                              */
/******************************************************************/

UINT Reason_GetParamCount (UINT nReason) 
{
  // verify params
  ASSERT (nReason<_REASON_COUNT) ;

  // verify result 
  ASSERT (aReasons[nReason].nParams<MAX_PARAMS) ;

  return aReasons[nReason].nParams ;
}


/******************************************************************/
/* Exported function : GetParamName                               */
/******************************************************************/

LPCTSTR Reason_GetParamName (UINT nReason, UINT nParam) 
{
  // verify params
  ASSERT (nReason<_REASON_COUNT) ;
  ASSERT (nParam<aReasons[nReason].nParams) ;

  return aReasons[nReason].aParams[nParam] ;
}


/******************************************************************/
/* Exported function : GetOptionMask                              */
/******************************************************************/

UINT Reason_GetOptionMask (UINT nReason) 
{
  // verify params
  ASSERT (nReason<_REASON_COUNT) ;
  
  if( nReason==REASON_SYS_EXECUTE || nReason==REASON_FILE_READ )
    return RULE_ASK | RULE_SCAN ;
  
  return RULE_ASK ;
}
